package impl;

import java.math.BigDecimal;

import api.IUserDataManager;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entities.Userphoto;
import entities.Userprofile;

/**
 * Session Bean implementation class UserDataManager
 */
@Stateless(name = "IUserDataManager")
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public class UserDataManager implements IUserDataManager {
	@PersistenceContext(unitName = "PhaseBookPU")
	EntityManager em;

	/**
	 * Default constructor.
	 */
	public UserDataManager() {
		// TODO Auto-generated constructor stub
	}

	private Userprofile getUserProfile(int uid) {

		return (Userprofile) em.find(Userprofile.class, uid);

	}

	@Override
	public boolean updateName(int uid, String name) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User name ***");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			System.out.println("*** Failed to update User Name ***");
			return false;
		}

		userProfile.setName(name);

		em.persist(userProfile);

		return true;
	}

	@Override
	public boolean updateAge(int uid, int age) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User age ***");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			System.out.println("*** Failed to update User Age ***");
			return false;
		}

		userProfile.setAge(new BigDecimal(age));

		em.persist(userProfile);

		return true;
	}

	@Override
	public boolean updateCity(int uid, String city) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User city ***");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			System.out.println("*** Failed to update User City ***");
			return false;
		}

		userProfile.setLocality(city);

		em.persist(userProfile);

		return true;
	}

	@Override
	public boolean updateGender(int uid, Gender sex) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User gender ***");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			System.out.println("*** Failed to update User Gender ***");
			return false;
		}

		userProfile.setGender(sex.equals(Gender.FEMALE) ? Gender.FEMALE.name()
				: Gender.MALE.name());

		em.persist(userProfile);

		return true;
	}

	@Override
	public boolean updateEmail(int uid, String email) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User email ***");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			System.out.println("*** Failed to update User Email ***");
			return false;
		}

		userProfile.setEmail(email);

		em.persist(userProfile);

		return true;
	}

	@Override
	public boolean updateNotificationApproach(int uid,
			NotificationApproach approach, boolean flag) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User Notification Approach ***");

		Userprofile userProfile = getUserProfile(uid);

		if (userProfile == null) {
			System.out
					.println("*** Failed to update User Notification Approach ***");
			return false;
		}

		if (approach.equals(NotificationApproach.ALL))
			userProfile.setNotify_OnChange(flag);

		if (approach.equals(NotificationApproach.DIGEST))
			userProfile.setNotify_Digest(flag);

		em.persist(userProfile);

		return true;
	}

	@Override
	public Userprofile getProfileById(int uid) {
		return getUserProfile(uid);
	}

	@Override
	public Userprofile getProfileByUsername(String username) {

		Query query = em.createNamedQuery("getUserProfile");
		query.setParameter("email", username);

		return (Userprofile) query.getSingleResult();

	}

	@Override
	public boolean updatePhoto(int uid, int photoId) {
		System.out.println("*** USERDATAMANAGER ***");
		System.out.println("*** Update User photo ***");

		Userprofile userProfile = getUserProfile(uid);
		Userphoto userPhoto = (Userphoto) em.find(Userphoto.class, photoId);

		if ((userProfile == null) || (userPhoto == null)) {
			System.out.println("*** Failed to update User Photo ***");
			return false;
		}

		userProfile.setPhotoid(userPhoto);

		em.persist(userProfile);

		return true;
	}

}
